/* ZetaWeb - ZetaMQ Web Interface
 *
 * Copyright (c) 2015 xiaohu <xiaohu@elecirc.com>.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
var express = require('express');
var path = require('path')
var favicon = require('serve-favicon')
var router = require('./routes/index');

var zetapp = express();

// view engine setup 
zetapp.set('views', path.join(__dirname, 'views'));
zetapp.set('view engine', 'jade');
//zetapp.engine('html', require('jade').__express);

// favicon
zetapp.use(favicon(__dirname + '/public/favicon.png'));

// public resources
zetapp.use(express.static(path.join(__dirname, 'public')));

// router
zetapp.use('/', router);

// catch 404 and forward to error handler
zetapp.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// development error handler
if (zetapp.get('env') === 'development') {
  zetapp.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// development error handler
zetapp.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

// module exports
module.exports = zetapp;
